package com.star.spring.deferred.service.completable;


import com.star.spring.deferred.utils.CommonUtils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @description: 综合案例
 * @author: star
 * @create: 2024-12-22 17:08
 **/

public class MultiDemo {

    public static void main(String[] args) {
        // 1 开启主线程
        CommonUtils.log("主线程开始");
//        String newsContent = "";
        // 2 读取本地新闻稿：news.txt文件
        CompletableFuture<String> newsFuture = CompletableFuture.supplyAsync(() -> {
            return CommonUtils.readFileToText("news.txt");
        });
        CompletableFuture<String> resultNews = newsFuture.thenApply(news -> {
            // 3 读取本地脏话词语汇总表:filter_word.txt文件
            String[] filterWordsArray = CommonUtils.readFileToTextArray("filter_words.txt");

            // 4 主线程中将新闻稿中脏话替换掉
            for (String word : filterWordsArray) {
                if (news.contains(word)) {
                    news = news.replace(word, "***");
                }
            }
            return news;
        });
        // 5 主线程中打印新闻稿
        try {
            CommonUtils.log(resultNews.get());
        } catch (Exception e) {
            CommonUtils.log(e.getMessage());
        }
        CommonUtils.log("主线程结束");
    }
}
